package org.jeecg.modules.flowermater.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.flower.entity.FlowerOutorder;
import org.jeecg.modules.flowermater.entity.FlowerMaterialBook;
import org.jeecg.modules.flowermater.entity.FlowerMaterialCost;
import org.jeecg.modules.flowermater.entity.FlowerMaterialCount;
import org.jeecg.modules.flowermater.service.IFlowerMaterialBookService;
import org.jeecg.modules.flowermater.service.IFlowerMaterialCountService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 采后物资统计
 */
@Api(tags = "采后物资统计")
@RestController
@RequestMapping("/flowermater/flowerMaterialCount")
@Slf4j
public class FlowerMaterialCountController extends JeecgController<FlowerMaterialBook, IFlowerMaterialBookService> {
    @Autowired
    private IFlowerMaterialCountService flowerMaterialCountService;


    /**
     * 物资盘点
     * @param storehouse
     * @param cost
     * @param sDate
     * @param eDate
     * @param req
     * @return
     */
    @AutoLog(value = "采后物资统计-物资盘点")
    @ApiOperation(value = "采后物资统计-物资盘点", notes = "采后物资统计-物资盘点")
    @GetMapping(value = "/queryMaterCount")
    public Result<?> queryMaterCount(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "cost", defaultValue = "") String cost,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            HttpServletRequest req) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(sDate)) {
            sDate = simpleDateFormat.format(calendar.getTime());
        }
        if (StringUtils.isBlank(eDate)) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            eDate = simpleDateFormat.format(calendar.getTime());
        }
        List<FlowerMaterialCount> flowerMaterialCounts = flowerMaterialCountService.queryMaterCount(storehouse, cost, sDate, eDate);
        return Result.ok(flowerMaterialCounts);
    }

    @RequestMapping(value = "/queryMaterCountExportXls")
    public ModelAndView exportXls(HttpServletRequest request,
                                  @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
                                  @RequestParam(name = "cost", defaultValue = "") String cost,
                                  @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                  @RequestParam(name = "eDate", defaultValue = "") String eDate) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(sDate)) {
            sDate = simpleDateFormat.format(calendar.getTime());
        }
        if (StringUtils.isBlank(eDate)) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            eDate = simpleDateFormat.format(calendar.getTime());
        }
        List<FlowerMaterialCount> flowerMaterialCounts = flowerMaterialCountService.queryMaterCount(storehouse, cost, sDate, eDate);

        // 过滤选中数据
        String selections = request.getParameter("selections");
        List<FlowerMaterialCount> flowerOutorderList = new ArrayList<FlowerMaterialCount>();
        if (oConvertUtils.isEmpty(selections)) {
            flowerOutorderList = flowerMaterialCounts;
        } else {
            List<String> selectionList = Arrays.asList(selections.split(","));
            flowerOutorderList = flowerMaterialCounts.stream().filter(item -> selectionList.contains( item.getId() )).collect(Collectors.toList());
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "采后物资盘点（存货明细表）"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerMaterialCount.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("采后物资盘点（存货明细表）", "导出人:" + sysUser.getRealname(), "采后物资盘点（存货明细表）"));
        mv.addObject(NormalExcelConstants.DATA_LIST, flowerOutorderList);
        return mv;
    }
}
